"""
    Author:
    Date: 2019-01-10

    CPS系统
"""

import random

from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from entities import sms_mgr
from entities.err import *

from . import helpers
from . import usecases
from . helpers import guild_login_required

from utils import ajax


def index(request):
    """ 公会管理后台首页 """
    err, context = usecases.index(request)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


def fetch_smscode(request):
    """ 获取验证码 """
    mobile = request.POST['mobile'].strip()
    if not mobile:
        return ajax.error_response('手机号码不可以为空!')

    sms_code = ''.join([random.choice('0123456789') for _ in range(4)])
    success = sms_mgr.send_sms_code(mobile, sms_code)
    if not success:
        return ajax.error_response('验证码发送失败!')

    return ajax.success_response({}, '验证码已发送')


def login(request, mobile, sms_code):
    """ 用户登录 """
    err = usecases.login(request, mobile, sms_code)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    redirect_url = reverse('guild:home')
    return HttpResponseRedirect(redirect_url)


@guild_login_required
def logout(request):
    """ 用户登出 """
    helpers.logout(request)
    redirect_url = reverse('guild:home')
    return HttpResponseRedirect(redirect_url)


@guild_login_required
def user_stat(request, begin, end):
    """ 用户统计详情 """
    err, context = usecases.user_list(request, begin, end)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def daily_stat(request):
    """ 每日统计 """
    err, context = usecases.daily_stat(request, 10)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def weekly_stat(request):
    """ 每周统计 """
    err, context = usecases.weekly_stat(request, 5)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def monthly_stat(request):
    """ 每月统计 """
    err, context = usecases.monthly_stat(request, 3)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def members(request):
    """ 公会成员列表 """
    err, context = usecases.members(request)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def caster_detail(request, uid):
    """ 主播个人详情 """
    err, context = usecases.caster_detail(request, uid)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def gallery(request, image):
    """ 头像的查看页面 """
    err, context = usecases.gallery(request, image)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def caster_video_list(request, uid):
    """ 获取用户的视频列表 """
    err, context = usecases.caster_video_list(request, uid)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def caster_album(request, uid):
    """ 个人相册 """
    err, context = usecases.caster_album(request, uid)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


@guild_login_required
def view_video(request, vid):
    """ 视频的查看页面 """
    err, context = usecases.view_video(request, vid)
    if err != ERROR_NO_ERROR:
        return helpers.show_warn_page(request, err['message'])

    return render(request, context['template'], context)


if __name__ == '__main__':
    print('ok')
